﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZY.EntityFrameWork.Core.Model.Entity.Authority;

namespace ZY.EntityFrameWork.Core.Services
{
    public abstract partial class BaseAuthorityService
    {
        public int AddNewUser(User user)
        {
            if(userRepository.CheckExists(q=>q.Id==user.Id||q.UserCode==user.UserCode))
            {
                throw new Exception("用户名或用户编号重复！");
            }
            return userRepository.Insert(user);
        }

        public bool CheckUser(string userCode, string passWord)
        {
            if (userRepository.CheckExists(q => q.UserCode == userCode && q.Password == passWord))
            {
                return true;
            }
            else
                return false;
        }

        public Role GetUserRole(string userName)//放在roleService中！！！
        {
            Role role = roleRepository.FindSingle(q => q.RoleName==userName);
            return role ?? null;
        }

        public List<User> GetAllUsers()
        {
            return userRepository.GetQueryable().ToList();
        }

        /// <summary>
        /// 获取每页的用户列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页行数</param>
        /// <param name="total">总用户数</param>
        /// <returns></returns>
        //public List<User> GetAllUsers(int pageIndex, int pageSize, out int total)
        //{

        //    var query = this.userRepository.GetQueryable();

        //    total = query.Count();
        //    query = query.OrderBy(u => u.Id)
        //        .Skip((pageIndex - 1) * pageSize)
        //        .Take(pageSize);

        //    return query.ToList();
        //}
    }
}
